define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'koala-search', 'shiftform' ,'modalSearch'],
function($, validate) {
  return {
    
    init: function() {
      this.initPage();
      this.initQuery();
      this.initModal();
    },
    
    initPage : function() {
    	
    	$(document).find('form').bind('reset', function(e){
            $(this).find(".select2").select2("val", "");
      	})
       
       $(document).on('click', function (e) {
           var $form = $(e.currentTarget).find("form");
           $form.bt_validate();
       });
      
      
      $('select[name="orgId"]').select2({
          theme: "classic",
          placeholder : '请选择',
          minimumResultsForSearch : -1,
          allowClear : true,
      });
      
      $('.btn-remove').on('click', function(e){
          $('input').val('');
          $('select[name="orgId"]').val(null).trigger("change");
      });
    },
    
    initModal: function() {
        
    	$(document).on("shown.bs.modal" , "#edit, #create" ,function(e){
            
      	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
            
        });
    },
    
    initQuery: function() {
      $(document).find('form').bt_validate();
      
      $('[data-target="#modal-search-fromloadplace"]').modalsearch({
          url: window.__CTX__.root + '/load-places/query',
          tableColumns: ['出发地代码', '出发地名称'],
          tableValues: ['code', 'name'],
          displayValues: ['code', 'name'],
          values: ['id']
        });
        
        $('[data-target="#modal-search-toloadplace"]').modalsearch({
          url: window.__CTX__.root + '/load-places/query',
          tableColumns: ['到达地代码', '到达地名称', 'id'],
          tableValues: ['code', 'name', 'id'],
          displayValues: ['code', 'name'],
          values: ['id']
        });
      
      var options = {
        time: 1000,
        url: window.__CTX__.root + "/load-places/query",
        queryName: "code",
        value: ["id"],
        display: ["code", "name"]
      }
      
      $('.fromloadplace-search').koalaSearch(options);
      $('.toloadplace-search').koalaSearch(options);
      
      $(document).on("koala.search.value.changed", ".fromloadplace-search, .toloadplace-search", function(){
        console.log($(this).next().val());
        // TODO 其他查询后的逻辑
      })
    }
  }
})